package com.ice.base.controller;

import com.ice.base.constant.BaseConstant;
import com.ice.base.model.SysMenu;
import com.ice.base.model.SysPermissionModel;
import com.ice.base.model.SysRoleModel;
import com.ice.base.model.SysRolePermissionModel;
import com.ice.base.service.*;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@ApiIgnore(value = "系统后台-授权管理")
public class SysRbacController extends BaseController {

    @Resource
    private SysPermissionService sysPermissionService;
    @Resource
    private SysRolePermissionService sysRolePermissionService;
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private SysRoleService sysRoleService;


    @ApiOperation(value = "RBAC-插入(给角色授权权限)2")
    @PostMapping("/base/RBAC/RolePermissionInsert2")
    public HashMap insert_RBACRolePermission2(@RequestParam("role_id") String role_id,
                                             @RequestParam("permission_id") String permission_id) {
        HashMap returnMap = getReturnMap(BaseConstant.Response_MENU.REQUEST_DO_SUCCESS);
        log.info("授权角色权限！");
        SysRolePermissionModel model = new SysRolePermissionModel();
        String[] split = permission_id.split(",");
        synchronized(this){
            sysRolePermissionService.deleteByRoleId(role_id);
            Arrays.stream(split).forEach(x->{
                model.init(model);
                model.setRole_id(role_id);
                model.setPermission_id(x);
                sysRolePermissionService.insert(model);
                }
            );
        }
        return returnMap;
    }


    @ApiOperation(value = "RBAC-插入(给角色授权权限)")
    @PostMapping("/base/RBAC/RolePermissionInsert")
    public HashMap insert_RBACRolePermission(@RequestParam("pId") String pId,//权限菜单的上级id
                                             @RequestParam("role_id") String role_id,
                                             @RequestParam("permission_id") String permission_id) {
        HashMap returnMap = getReturnMap(BaseConstant.Response_MENU.REQUEST_DO_SUCCESS);
        log.info("授权角色权限！");
        Boolean flag = true;
        SysRolePermissionModel sysRolePermissionModel = sysRolePermissionService.selectOneByPermissionIdAndRoleId(permission_id, role_id);
        if(null != sysRolePermissionModel){
            flag = false;
            log.info("重复授权~permission_id："+permission_id);
        }
        try{
            SysRolePermissionModel model = new SysRolePermissionModel();
            if(flag){
                //删除权限角色中间表
                // Integer integer = sysRolePermissionService.deleteByPermissionIdAndRoleId(permission_id, role_id);
                model.init(model);
                model.setRole_id(role_id);
                model.setPermission_id(permission_id);
                model.setPermission_name(sysPermissionService.selectById(permission_id).getPermission_name());
                sysRolePermissionService.insert(model);
                log.info("授权角色新增roleid："+role_id +",permission_id:"+permission_id);
            }
        }catch (Exception e ){
            e.printStackTrace();
        }

        return returnMap;
    }


    @ApiOperation(value = "RBAC-删除角色权限")
    @PostMapping("/base/RBAC/RolePermissionDeleteById")
    public HashMap deleteById_RBACRolePermission(@RequestParam("role_id") String role_id,
                                                 @RequestParam("permission_id") String permission_id) {
        return getHashMap(sysRolePermissionService.deleteByPermissionIdAndRoleId(permission_id, role_id));
    }

    @ApiOperation(value = "角色-查询角色下的菜单权限")
    @PostMapping("/base/RBAC/selectPermissionsByRoleId")
    public HashMap selectByRoleId_RBAC(@RequestParam("role_id") String role_id) {
        HashMap<String, Object> resultMap = getReturnMap(BaseConstant.Response_MENU.REQUEST_DO_SUCCESS);
        //查询一个角色
        try {
            SysRoleModel sysRoleModel = sysRoleService.selectDetailsById(role_id);
            ArrayList<SysMenu> sysMenuModels = sysMenuService.selectAll();
            ArrayList<Object> resultDataList = new ArrayList<>();
            if (sysRoleModel != null) {
                List<SysPermissionModel> roles = sysRoleModel.getPermissions();
                if (roles != null) {
                    createRBACPermission(sysMenuModels, resultDataList, roles);
                    resultMap.put("data", resultDataList);
                    getReturnMapSuccess(resultMap);
                }
            }
        }catch (Exception e ){
            e.printStackTrace();
            resultMap = getReturnMap(BaseConstant.Response_MENU.REQUEST_DO_FAILED);

        }
        return resultMap;
    }



    private void createRBACPermission(ArrayList<SysMenu> sysMenuModels, ArrayList<Object> resultDataList, List<SysPermissionModel> Permissions) {
        for (SysPermissionModel permissionModel : Permissions) {
            HashMap<String, Object> dataMap = new HashMap<>();
            if (sysMenuModels != null) {
                for (SysMenu sysMenuModel : sysMenuModels) {
                    if (sysMenuModel.getId().equals(permissionModel.getPermission_band_menu_id())) {
                        dataMap.put("id", permissionModel.getId());
                        dataMap.put("name", permissionModel.getPermission_name());

                        dataMap.put("tid", sysMenuModel.getId());
                        if ("无".equals(sysMenuModel.getMenu_parent_id())) {
                            dataMap.put("isParent", true);
                        } else
                            dataMap.put("tpid", sysMenuModel.getMenu_parent_id());

                        resultDataList.add(dataMap);
                        break;
                    }
                }
            }
        }
    }





    private HashMap getHashMap(Integer integer2) {
        HashMap<String, Object> returnMap = getReturnMap();
        if (integer2 == 1) {
            getReturnMapSuccess(returnMap);
        }
        return returnMap;
    }
}
